#include <stdlib.h>

#include "lib.h"

#include "slotguts.h"
#include "lightspeedguts.h"
#include "lightspeedslotscommon.h"
#include "slotspin.h"
#include "getrand.h"


bool8 
LightSpeedSlotGuts::isValidGuts()
{
    return getGutsId() >= 1000 && getGutsId() < 1100;
}

void
LightSpeedSlotGuts::loadSubGameData()
{
	SlotGuts::loadSubGameData();

	// If the speedfactor is not set, default it.
	if ( m_subGame->getData(LightSpeedCommon::SGIDX_SPEEDFACTOR) < 1 )
    {
        m_subGame->update(LightSpeedCommon::SGIDX_SPEEDFACTOR, 1);
    }
}

uint8
LightSpeedSlotGuts::classifyStops(uint8* stops,uint8 bet, uint8 line)
{
	uint8 result = LightSpeedCommon::EVT_LOSER;
    int numAliens = 0;
    int numSpaceships = 0;
    int numStars = 0;
    int numEarths = 0;
    int numRayGuns = 0;
    int numWilds = 0;
    int numWarps = 0;
	bool8 isMaxBet = (this->getBetCount() == bet);
    int32 speedFactor = m_subGame->getData(LightSpeedCommon::SGIDX_SPEEDFACTOR);
    int32 newSpeedFactor = 1;

    //  Do a preliminary run-through
    for ( uint8 i = 0; i < getReelCount(); i++ )
    {
        // Set the icon flags;
        switch ( getIcon(i, stops[i])->iconid )
        {
            case LightSpeedCommon::ICON_WARP:
                numWarps++;
                break;

            case LightSpeedCommon::ICON_WILD:
                numWilds++;
                break;

            case LightSpeedCommon::ICON_ALIEN:
                numAliens++;
                break;

            case LightSpeedCommon::ICON_SPACESHIP:
                numSpaceships++;
                break;

            case LightSpeedCommon::ICON_STAR:
                numStars++;
                break;

            case LightSpeedCommon::ICON_EARTH:
                numEarths++;
                break;

            case LightSpeedCommon::ICON_RAYGUN:
                numRayGuns++;
                break;
        }
    }

    if ( result == LightSpeedCommon::EVT_LOSER && numWarps > 0 )
    {
        // Modify by the number of warps and wilds (adjusting for 0-based).
        // Also update the subgame data.
        result = LightSpeedCommon::EVT_1WARP + numWarps - 1;

		if(isMaxBet)
		{
			speedFactor += (numWarps);
			if(speedFactor > 7)
			{
				speedFactor = 7;
			}
		}
    }

    // EVT_RAYGUNS
    if ( result == LightSpeedCommon::EVT_LOSER && (numRayGuns == 3 || numRayGuns + numWilds == 3) )
    {
        result = LightSpeedCommon::EVT_3RAYGUNS;
    }    
	
	// EVT_ALIENS
    if ( result == LightSpeedCommon::EVT_LOSER && (numAliens == 3 || numAliens + numWilds == 3) )
    {
        result = LightSpeedCommon::EVT_3ALIENS;
    }

    // EVT_SPACESHIPS
    if ( result == LightSpeedCommon::EVT_LOSER && (numSpaceships == 3 || numSpaceships + numWilds == 3) )
    {
        result = LightSpeedCommon::EVT_3SPACESHIPS;
    }

    // EVT_STARS
    if ( result == LightSpeedCommon::EVT_LOSER && (numStars == 3 || numStars + numWilds == 3) )
    {
        result = LightSpeedCommon::EVT_3STARS;
    }

    // EVT_3EARTHS
    if ( result == LightSpeedCommon::EVT_LOSER && (numEarths == 3 || numEarths + numWilds == 3) )
    {
        result = LightSpeedCommon::EVT_3EARTHS;
    }

	//Only maxbet play affects and is affected by the light speed factor
	if(isMaxBet)
	{
		// Determine the new speed factor BEFORE we modify the result.
        if ( result != LightSpeedCommon::EVT_LOSER && numWarps == 0 )
        {
            newSpeedFactor = 1;
        }
        else
        {
            newSpeedFactor = speedFactor;
        }

		// Update the result by the current light speed factor.
		result = result + (m_subGame->getData(LightSpeedCommon::SGIDX_SPEEDFACTOR) - 1) * (LightSpeedCommon::NUMEVENTS);

		// Update the subgamedata with the new lightspeed factor.
		m_subGame->update(LightSpeedCommon::SGIDX_SPEEDFACTOR, newSpeedFactor);
	}

	return result;
}
